<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { useUserStore } from '../stores/user'

const router = useRouter()
const userStore = useUserStore()

// 硬编码的用户账号和密码
const users = {
  admin: { username: 'admin', password: 'admin123', type: 'admin' },
  visitor: { username: 'visitor', password: 'visitor123', type: 'visitor' }
}

const loginForm = reactive({
  username: '',
  password: ''
})

const loginFormRef = ref<FormInstance>()

const rules = reactive<FormRules>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应在3到20个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应在6到20个字符之间', trigger: 'blur' }
  ]
})

const loading = ref(false)

const handleLogin = async (formEl: FormInstance | undefined) => {
  if (!formEl) return

  await formEl.validate((valid) => {
    if (valid) {
      loading.value = true

      // 模拟登录请求延迟
      setTimeout(() => {
        const { username, password } = loginForm

        // 验证用户名和密码
        if (username === users.admin.username && password === users.admin.password) {
          // 设置用户信息到store
          userStore.setUserInfo({
            username: username,
            type: 'admin'
          })
          ElMessage.success('管理员登录成功')
          router.push('/blank')
        } else if (username === users.visitor.username && password === users.visitor.password) {
          // 设置用户信息到store
          userStore.setUserInfo({
            username: username,
            type: 'visitor'
          })
          ElMessage.success('游客登录成功')
          router.push('/blank')
        } else {
          ElMessage.error('用户名或密码错误')
        }

        loading.value = false
      }, 1000)
    }
  })
}
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h1>博物馆参观系统</h1>
        <div class="museum-icon">
          <img src="../assets/museum-logo.svg" alt="博物馆图标" />
        </div>
      </div>

      <div class="account-info">
        <h3>测试账号</h3>
        <div class="account-list">
          <div class="account-item">
            <span class="account-label">管理员：</span>
            <span class="account-value">admin / admin123</span>
          </div>
          <div class="account-item">
            <span class="account-label">游客：</span>
            <span class="account-value">visitor / visitor123</span>
          </div>
        </div>
      </div>

      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="rules"
        class="login-form"
        label-position="top"
      >


        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
          />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            class="login-button"
            :loading="loading"
            @click="handleLogin(loginFormRef)"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>

      <div class="login-footer">
        <p>© {{ new Date().getFullYear() }} 博物馆参观系统 - 保留所有权利</p>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100vw;
  margin: 0;
  padding: 0;
  background-image: url('https://img.freepik.com/free-photo/empty-museum-hall-with-spotlights_107420-95998.jpg');
  background-size: cover;  // 修改为100% 100%来完全铺满
  background-position: center;
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
  }
}

.login-box {
  width: 450px;
  padding: 40px;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  position: relative;
  z-index: 1;

  .login-header {
    text-align: center;
    margin-bottom: 30px;

    h1 {
      font-size: 28px;
      color: #333;
      margin-bottom: 15px;
      font-family: 'SimSun', serif;
      letter-spacing: 2px;
    }

    .museum-icon {
      width: 80px;
      height: 80px;
      margin: 0 auto;
      background-color: #f5f5f5;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;

      img {
        width: 50px;
        height: 50px;
      }
    }
  }

  .login-form {
    margin-bottom: 20px;

    .el-form-item {
      margin-bottom: 20px;
    }

    .login-button {
      width: 100%;
      padding: 12px 0;
      font-size: 16px;
      background-color: #8b5a2b;
      border-color: #8b5a2b;

      &:hover, &:focus {
        background-color: #a67c52;
        border-color: #a67c52;
      }
    }
  }

  .login-footer {
    text-align: center;
    font-size: 12px;
    color: #666;
  }

  .account-info {
    margin-bottom: 20px;
    padding: 15px;
    background-color: rgba(139, 90, 43, 0.1);
    border-radius: 6px;
    border-left: 3px solid #8b5a2b;

    h3 {
      margin-top: 0;
      margin-bottom: 10px;
      font-size: 16px;
      color: #8b5a2b;
    }

    .account-list {
      .account-item {
        margin-bottom: 5px;
        font-size: 14px;
        display: flex;

        &:last-child {
          margin-bottom: 0;
        }

        .account-label {
          font-weight: bold;
          color: #555;
          min-width: 70px;
        }

        .account-value {
          color: #8b5a2b;
          font-family: monospace;
          font-weight: bold;
        }
      }
    }
  }
}

// 添加一些博物馆风格的装饰元素
.login-box::before,
.login-box::after {
  content: '';
  position: absolute;
  width: 30px;
  height: 30px;
  border: 2px solid #8b5a2b;
  z-index: -1;
}

.login-box::before {
  top: -10px;
  left: -10px;
  border-right: none;
  border-bottom: none;
}

.login-box::after {
  bottom: -10px;
  right: -10px;
  border-left: none;
  border-top: none;
}

// 响应式调整
@media (max-width: 768px) {
  .login-box {
    width: 90%;
    padding: 30px;
  }
}
</style>